package cacco.jp.mld;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cacco.jp.mld.panel.entity.ChildEntity;
import cacco.jp.mld.panel.form.JqgridResponse;
import cacco.jp.mld.panel.form.StatusResponse;
import cacco.jp.mld.panel.service.ChildService;

@Controller
@RequestMapping(value = "/childs")
public class ChildController {

	private static final Logger logger = LoggerFactory
			.getLogger(ChildController.class);

	@Autowired
	private ChildService childService;

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String listHome(ModelMap map, HttpServletRequest request) {
		map.addAttribute("employee", new ChildEntity());
		map.addAttribute("list_url", childService.getAll());
		ChildEntity entity = (ChildEntity) request.getSession()
				.getAttribute("aa");
		map.addAttribute("session", entity);
		System.out.println(entity);

		return "child/childs";
	}

	@RequestMapping(value = "/childs", method = RequestMethod.GET)
	public String listChild(ModelMap map, HttpServletRequest request) {
		map.addAttribute("employee", new ChildEntity());
		map.addAttribute("list_url", childService.getAll());
		ChildEntity entity = (ChildEntity) request.getSession()
				.getAttribute("aa");
		map.addAttribute("session", entity);
		System.out.println(entity);

		return "child/childs";
	}

	@RequestMapping(value = "/listgrid", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public JqgridResponse<ChildEntity> listGrid(
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "rows", required = false) Integer rows,
			@RequestParam(value = "sidx", required = false) String sortBy,
			@RequestParam(value = "sord", required = false) String order) {

		logger.info("Listing items for grid with page: {}, rows: {}", page,
				rows);
		logger.info("Listing items for grid with sort: {}, order: {}", sortBy,
				order);

		List<ChildEntity> list;
		list = childService.getAll();//childService.getAlByPage(rows, page, sortBy, order);
		JqgridResponse<ChildEntity> response = new JqgridResponse<ChildEntity>();
		int totalRecords = childService.getTotalRecords();
		int total = totalRecords % rows == 0 ? (int) Math.ceil(totalRecords
				/ rows) : (int) Math.ceil(totalRecords / rows) + 1;

		logger.info("Listing child urls for grid with total rows {}", list.size());
		response.setRecords(list);
		response.setTotalRecords(totalRecords);
		response.setTotalPages(total);
		response.setCurrentPage(page);
		return response;
	}

	@RequestMapping(value = "/add", produces = "application/json", method = RequestMethod.POST)
	public @ResponseBody
	StatusResponse create(
			) {
		ChildEntity child = new ChildEntity();
		StatusResponse response = new StatusResponse();
		try{
			childService.addChild(child);
			response.setSuccess(true);
			logger.info("Added new child url: {}", child.getId());
		} catch (Exception ex) {
			response.setSuccess(false);
			response.setMessage("Add child url Failed");
			logger.info("Add child url Failed");
		}
		return new StatusResponse(true);
	}

	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	@ResponseBody
	public StatusResponse update(@RequestBody ChildEntity child) {
		StatusResponse respone = new StatusResponse();
		try {
			childService.updateChild(child);
			respone = new StatusResponse(true);
			logger.info("Updated child url: {}", child.getId());
		} catch (Exception e) {
			respone = new StatusResponse(false);
			respone.setMessage("Update failed");
			logger.info("Child url is not exists: {}", child.getId());
		}
		return respone;
	}

	@RequestMapping(value = "/delete", produces = "application/json", method = RequestMethod.POST)
	public @ResponseBody
	StatusResponse delete(@RequestParam Integer id) {
		StatusResponse response = new StatusResponse();
		try {
			childService.deleteChild(id);
			response.setSuccess(true);
			logger.info("Deleted child url: {}", id);
		} catch (Exception e) {
			response.setSuccess(false);
			logger.info("Delete failed");
		}
		return response;
	}
}
